package main;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;


/**
 * last change:  $Date$
 * by:           $Author$
 * revision:     $Revision$
 *
 */
public class ServerConfig {
  private int port;
  private Map<String, String> translations;
  private boolean translationsPart;
  private String databaseUsername;
  private String databasePassword;
  private String databaseUrl;
  
  public ServerConfig() {
    translations = new HashMap<String, String>();
    translationsPart = false;
    try{
      FileInputStream fstream = new FileInputStream("src/main/resources/server-config.cfg");
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;

      while ((strLine = br.readLine()) != null) {
        if(strLine.trim().equals("-port-")) {
          if((strLine = br.readLine()) != null) {
            port = Integer.parseInt(strLine.trim());
          }
        }
        if(strLine.trim().equals("-database-username-")) {
          if((strLine = br.readLine()) != null) {
            databaseUsername = strLine.trim();
          }
        }
        if(strLine.trim().equals("-database-password-")) {
          if((strLine = br.readLine()) != null) {
            databasePassword = strLine.trim();
          }
        }
        if(strLine.trim().equals("-database-url-")) {
          if((strLine = br.readLine()) != null) {
            databaseUrl = strLine.trim();
          }
        }
        
        if(translationsPart) {
          String[] parsedTranslation = strLine.split(":");
          translations.put(parsedTranslation[0], parsedTranslation[1]);
        }
        if(strLine.trim().equals("-translations-")) {
          translationsPart = true;
        }
      }
      
      //Close the input stream
      in.close();
        } catch (Exception e) {
          System.err.println("Error: " + e.getMessage());
      }
  }

  public int getPort() {
    return port;
  }

  public void setPort(int port) {
    this.port = port;
  }

  public Map<String, String> getTranslations() {
    return translations;
  }

  public void setTranslations(Map<String, String> translations) {
    this.translations = translations;
  }

  public String getDatabaseUsername() {
    return databaseUsername;
  }

  public void setDatabaseUsername(String databaseUsername) {
    this.databaseUsername = databaseUsername;
  }

  public String getDatabasePassword() {
    return databasePassword;
  }

  public void setDatabasePassword(String databasePassword) {
    this.databasePassword = databasePassword;
  }

  public String getDatabaseUrl() {
    return databaseUrl;
  }

  public void setDatabaseUrl(String databaseUrl) {
    this.databaseUrl = databaseUrl;
  }
  
}
